//
//  HistorySearchView.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/27.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "HistorySearchView.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "HistoryViewCell.h"
#import "HistoryHeaderReusableView.h"
#import "DY_searchHistoryDataBase.h"

@interface HistorySearchView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionVeiw;

@end

@implementation HistorySearchView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionVeiw];
        
    }
    return self;
}



- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    WS(weakSelf);
    [self.collectionVeiw performBatchUpdates:^{
        [weakSelf.collectionVeiw reloadSections:[NSIndexSet indexSetWithIndex:0]];
    } completion:nil];

}

- (void)setSectionArray:(NSMutableArray *)sectionArray {
    _sectionArray = sectionArray;
    WS(weakSelf);
    [self.collectionVeiw performBatchUpdates:^{
        [weakSelf.collectionVeiw reloadSections:[NSIndexSet indexSetWithIndex:1]];
    } completion:nil];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return section == 0 ? self.dataArray.count : self.sectionArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf);
    NSArray *array = @[@"历史搜索",@"搜索推荐"];
    HistoryHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HistoryHeaderReusableView" forIndexPath:indexPath];
    headerView.sectionLabel.text = array[indexPath.section];
    headerView.deleteButton.hidden = indexPath.section == 0 ? NO : YES;
    headerView.deleteButtonClickBlock = ^{
        weakSelf.dataArray = @[];
        [[DY_searchHistoryDataBase shareDataBase] deleteLocalDataArray];
        [weakSelf.collectionVeiw reloadData];
        if (weakSelf.deleteIndexBlock) {
            weakSelf.deleteIndexBlock();
        }
    };
    return headerView;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HistoryViewCell" forIndexPath:indexPath];
    cell.titleLabel.text = indexPath.section == 0 ? self.dataArray[indexPath.row] : self.sectionArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.searchIndexBlock) {
        self.searchIndexBlock(indexPath.section==0 ? self.dataArray[indexPath.row] : self.sectionArray[indexPath.row]);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake([self boundingStr:indexPath.section == 0 ? self.dataArray[indexPath.row] : self.sectionArray[indexPath.row] andFontSize:font(24)]+Scale_W(20), Scale_W(25));  //设置cell宽高
}

- (CGFloat)boundingStr:(NSString *)Str andFontSize:(CGFloat)fontSize{
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    CGSize size = [Str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;
    return size.width;
}


- (UICollectionView *)collectionVeiw {
    if (!_collectionVeiw) {
        UICollectionViewLeftAlignedLayout *flowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; //设置竖直滚动
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, Scale_W(30));
        flowLayout.minimumLineSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionVeiw = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        _collectionVeiw.backgroundColor = [UIColor whiteColor];
        _collectionVeiw.dataSource = self;
        _collectionVeiw.delegate = self;
        [_collectionVeiw registerClass:[HistoryViewCell class] forCellWithReuseIdentifier:@"HistoryViewCell"];
        [_collectionVeiw registerClass:[HistoryHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HistoryHeaderReusableView"];
    }
    return _collectionVeiw;
}



@end
